<?php

include_once("/var/www/html/new/com.inc.php");
include("common/header.html");
include("/var/www/html/new/conf/yd.le.je");
$ydle=new yd_go_sql();
$ydle->connect(); 
//保存图片 记录图片地址 存入数据库 最后跳转到刚才写的游记页面游记
$topic = $_REQUEST['topic'];
$playtime =$_REQUEST['playtime'];
//echo $playtime;
$fid=$_SESSION['memberID'];
$text = $_REQUEST['text'];
if($topic==""||$playtime==""||$text==""){
    echo "<script>alert('标题，内容，时间不能为空!');</script>";
    exit;
}
/*$ph = $_FILES['photo'];

if($ph){
if($_SERVER['REQUEST_METHOD']!=="POST"){
	echo "<script>alert(\"传输类型不是POST!\");</script>";
	echo "<script language=\"javascript\">";
	echo "document.location=\"".$GLOBALS['_SERVER']['HTTP_REFERER']."\"";
	echo "</script>";
	exit;
}
if(!$ph['size']){
	echo "<script>alert(\"没有上传文件!\");</script>";
	echo "<script language=\"javascript\">";
	echo "document.location=\"".$GLOBALS['_SERVER']['HTTP_REFERER']."\"";
	echo "</script>";
	exit;
}
if(count($ph['name'])<1){
	echo "<script>alert(\"上传图片的数量不能少于1张!\");</script>";
	echo "<script language=\"javascript\">";
	echo "document.location=\"".$GLOBALS['_SERVER']['HTTP_REFERER']."\"";
	echo "</script>";
	exit;
	}
$type=array("image/gif","image/jpeg","image/pjpeg","image/png","image/x-png");
foreach ($ph['type'] as  $value) {	
	if(!in_array($value,$type)){
	echo "<script>alert(\"上传文件类型不合法!\");</script>";
	echo "<script language=\"javascript\">";
	echo "document.location=\"".$GLOBALS['_SERVER']['HTTP_REFERER']."\"";
	echo "</script>";
	exit;
	}
}
$maxsize=2000000;
foreach ($ph['size'] as  $value) {
	if($value>$maxsize){
	echo "<script>alert(\"上传文件过大!\");</script>";
	echo "<script language=\"javascript\">";
	echo "document.location=\"".$GLOBALS['_SERVER']['HTTP_REFERER']."\"";
	echo "</script>";
	exit;
	}
}
foreach ($ph['error'] as  $value) {	
	if($value>0){
	echo "<script>alert(\"".$value."\");</script>";
	echo "<script language=\"javascript\">";
	echo "document.location=\"".$GLOBALS['_SERVER']['HTTP_REFERER']."\"";
	echo "</script>";
	exit;
	}
}
//按帐号来设置放图片的路径
$passport=$_SESSION['account'];
$dir="http://www.12301.cc/yd_prodimgs/".$passport;
if(!is_dir($dir)){//没有上传过图片的话先建目录
	mkdir($dir);
}					
$fulldir=array();
$strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
for ($i=0; $i < count($ph['name']); $i++) { 	
	if($ph['name'][$i]){						
        $filename=time().$strPol[rand(0,61)].$strPol[rand(0,61)];//用时间戳和随机数来改写图片名称
        $houzui=strrchr($ph['name'][$i],'.');							
        $fulldir[$i]="http://www.12301.cc/yd_prodimgs/".$passport."/".$filename.$houzui;//完整路径
        move_uploaded_file($ph["tmp_name"][$i],$fulldir[$i]);//上传
        }                   
}
// print_r($fulldir);//把数组压缩成一个字符串存进去
$fulldir=implode('|', $fulldir);
}
// print_r($fulldir);
//写入数据库

$pic =$fulldir;*/
$pattern="/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png]))[\'|\"].*?[\/]?>/";
preg_match_all($pattern,$text,$match);
$pic=$match[1][0];
if(!$pic){
    //echo "<script>alert('至少包含一张图片!');parent.window.location='http://mx.12301.cc/yb/member.php';</script>";//
    echo '至少包含一张图片';
    exit;
}
// if(!p_match($text)){

	if($_REQUEST['action']=="发布"){//发布游记
		$sql = "insert into uu_blog(topic,playtime,fid,content,pic,status) values('$topic','$playtime','$fid','$text','$pic','2')";
		$GLOBALS['ydle']->query($sql);
		echo "<script>alert('发布成功!');parent.window.location='blog.html';</script>";
		
	}elseif($_REQUEST['action']=="保存"){//保存游记
		$sql = "insert into uu_blog(topic,playtime,fid,content,status) values('$topic','$playtime','$fid','$text','1')";
		$GLOBALS['ydle']->query($sql);
		echo "<script>alert('保存成功!');parent.window.location='blog.html';</script>";
	}

    

// }else{
	// echo "输入不合法";
// }
exit();
//跳回游记页面
$sql = "SELECT id from uu_blog where fid=$'fid' order by id desc limit 1";
$GLOBALS['ydle']->query($sql);
$GLOBALS['ydle']->fetch_assoc();
$id = $GLOBALS['ydle']->f('id');
echo "<script language=\"javascript\">";
echo "document.location=\"yd.12301.cc/blog.html?id=".$id."\"";
echo "</script>";
?>